import router from './router'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { getToken } from '@/utils/auth'
import { setImpactType, setImpactCode } from '@/utils/auth'
import { useConfigStore } from '@/stores/config';
const whiteList = ['/login', '/agreement', '/register', '/forgetPwd']

NProgress.configure({
  showSpinner: false
});

// 路由前置守卫：在每次路由跳转前执行
router.beforeEach((to, from, next) => {
  NProgress.start();

  if (getToken()) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      useConfigStore().getParams();
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`)
    }
  }

  let type = to.query.impactType;
  let code = to.query.impactCode;
  // console.log(type, code);
  if (type && code) {
    setImpactType(type);
    setImpactCode(code);
  }

  NProgress.done();
});

// 路由后置守卫：在每次路由跳转完成后执行
router.afterEach((to, from) => {
  NProgress.done();
});